<template>
  <div class="about">
    <div v-for="item in store.goods" :key="item.id">
        {{ item.name }}
    </div>
    <br/>
    <el-cascader 
    :show-all-levels="false"
    v-model="good.categoryId" :options="options" :props="{'label':'catename','value':'id',multiple: true}" @change="handleChange" />
    <p>{{ good.categoryId}}</p>
    <el-upload
    v-model:file-list="good.gallery"
    action="http://dida100.com:8888/api/file/upload"    
    :show-file-list = false
  >
  <template #trigger>
      <el-button type="primary">上传图片</el-button>
    </template>
  </el-upload>
    <el-button @click="showGallery=true;getUpImg()">服务器选择</el-button>
     
    <el-dialog v-model="showGallery" title="Shipping address">
      <div style="display:flex;width:100%;flex-wrap:wrap">
      <div v-for="item in imgList" :key="item.id" >
        <el-checkbox   @change="checkChange($event,item)"  size="large" />
        <img :src="item.url"  width="100" />
        <p>{{ item.name }}</p>
      </div>
         
      </div>
      
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="showGallery = false">Cancel</el-button>
        <el-button type="primary" @click="setGallery()">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
  <p>图片 {{ good.gallery }}</p>
    <img v-for="item in good.gallery" width="100" :src="item.url" :key="item.id"/>
  </div>

  <el-upload
    v-model:file-list="good.gallery"
    action="http://dida100.com:8888/api/file/upload"
    list-type="picture-card"
    :on-preview="handlePictureCardPreview"
    :on-remove="handleRemove"
  >
   
    <el-icon><Plus /></el-icon>
  </el-upload>

  <el-dialog v-model="dialogVisible">
    <img w-full :src="dialogImageUrl" alt="Preview Image" />
  </el-dialog>
</template>
<script lang="ts" setup>
import { Plus } from '@element-plus/icons-vue'

import type { UploadProps, UploadUserFile } from 'element-plus'
import { ref } from 'vue'
import {useCarStore} from '@/stores/cart' 
import {GetCategory,GetUpImg} from '@/api/index'

const store = useCarStore();
store.getCart();
interface imgType  {
  id:number
  url:string
  name:string
}
const imgList = ref <imgType[]>([])
const selList = ref <imgType[]>([]);
const options = ref([]);
const showGallery =ref(false)
const good = ref({
  "sku": "6",
  "isShow": 1,
  "cateName": "",
  "categoryId": '',
  "shopId": 1,
  "shopName": "九龙城店",
  "productName": "寿光大西红柿",
  "gallery": [] as imgType[],
  "produceId": "6954767410388",
  "online": 1,
  "onlineUnified": 1,
  "stock": 25,
  "originalPrice": 4.5,
  "price": 3.5,
  "fee": 1,
  "StorageConditions": "",
  "brand": "寿光",
  "origin": "寿光",
  "keyword": "西红柿",
  "descript": "好西红柿寿光造",
  "body": "好西红柿寿光造"
})

const props = {
  expandTrigger: 'hover' as const,
}

const handleChange = (value:Event) => {
  console.log(value)
}

GetCategory()
.then(res=>{
  options.value = res.data.list;
})

function getUpImg(){
  GetUpImg({})
  .then(res=>{
    imgList.value = res.data.data;
  })
}

function checkChange(e:any,item:imgType){
  console.log(e);
  if(e){
      selList.value.push(item)
  }else{
     var ind = selList.value.findIndex(value=>value.id==item.id)
     selList.value.splice(ind,1);
  }
}
function setGallery(){
  good.value.gallery = [...selList.value]
  showGallery.value = false;
}



 

const dialogImageUrl = ref('')
const dialogVisible = ref(false)

const handleRemove: UploadProps['onRemove'] = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles)
}

const handlePictureCardPreview: UploadProps['onPreview'] = (uploadFile) => {
  dialogImageUrl.value = uploadFile.url!
  dialogVisible.value = true
}


</script>

<style>
@media (min-width: 1024px) {
  
}
</style>
